package jdbc.exer.dao;

import jdbc.exer.domain.Subject;
import jdbc.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author shkstart
 * @creat 2022-05-9:35
 */
public class SubjectDAO {
    public int add(Subject s){
        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DButil.getCon();
            Scanner sc = new Scanner(System.in);

            String sql = "insert into s_subject(subjectname,classhour,gradeid) values(?,?,?)";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            st.setString(1,s.getSubjectname());
            st.setInt(2,s.getClasshour());
            st.setLong(3,s.getGradeid());

            return st.executeUpdate();
        } catch (SQLException e) {
            //e.printStackTrace();
            DButil.handlerEx(e);
        } finally {
            DButil.close(null, st, connection);
        }
        return  0;
    }
    public int update(Subject s){

        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DButil.getCon();
            Scanner sc = new Scanner(System.in);

            String sql = "update s_subject set sujectname=?,classhour=?,gradeid=? where id=?";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            st.setString(1,s.getSubjectname());
            st.setInt(2,s.getClasshour());
            st.setLong(3,s.getGradeid());
            st.setLong(4,s.getId());
            return st.executeUpdate();
        } catch (SQLException e) {
            //e.printStackTrace();
            DButil.handlerEx(e);
        } finally {
            DButil.close(null, st, connection);
        }
        return  0;
    }
    public int del(long id){
        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DButil.getCon();
            Scanner sc = new Scanner(System.in);

            String sql = "delete s_subject where id=?";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            st.setLong(1,id);
            return st.executeUpdate();
        } catch (SQLException e) {
            //e.printStackTrace();
            DButil.handlerEx(e);
        } finally {
            DButil.close(null, st, connection);
        }
        return  0;
    }
    //主键找对象
    public Subject get(long id){
        Connection connection = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try {
            connection = DButil.getCon();
            Scanner sc = new Scanner(System.in);

            String sql = "select * from s_subject where id=?";
            System.out.println(sql);
            st = connection.prepareStatement(sql);
            st.setLong(1,id);
            rs = st.executeQuery();
            if(rs.next()){
                return new Subject(rs.getLong(1),rs.getString(2),rs.getInt(3),rs.getLong(4));
            }
        } catch (SQLException e) {
            DButil.handlerEx(e);
            //e.printStackTrace();
        } finally {
            DButil.close(rs, st, connection);
        }

        return null;
    }
    //查询全部对象
    public List<Subject> findALL(){
        Connection connection = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        List<Subject> subjectList = new ArrayList<>();
        try {
            connection = DButil.getCon();
            Scanner sc = new Scanner(System.in);

            String sql = "select * from s_subject ";
            System.out.println(sql);
            st = connection.prepareStatement(sql);

            rs = st.executeQuery();
            while(rs.next()){
                subjectList.add(new Subject(rs.getLong(1),rs.getString(2),rs.getInt(3),rs.getLong(4)));
            }
        } catch (SQLException e) {
            DButil.handlerEx(e);
            //e.printStackTrace();
        } finally {
            DButil.close(rs, st, connection);
        }
        return subjectList;
    }

    public List<Subject> search(int pageNo,int pageSize) {
        String sql = "select * from s_subject order by id asc limit ?,?";
        List<Subject> datas = new ArrayList<>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            con = DButil.getCon();
            ps = con.prepareStatement(sql);
            ps.setInt(1,(pageNo-1)*pageSize);
            ps.setInt(2,pageSize);
            rs = ps.executeQuery();
            while (rs.next()) {
                datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"), rs.getLong("gradeid")));
            }
        } catch (SQLException e) {
            DButil.handlerEx(e);
            //e.printStackTrace();
        } finally {
            DButil.close(rs, ps, con);
        }
        return datas;
    }

}
